package org.hxyjs.dao.impl;

import org.hxyjs.dao.AdministratorTypeDao;
import org.hxyjs.entity.AdministratorType;
import org.hxyjs.mapper.AdministratorTypeMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("administratorTypeDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class AdministratorTypeDaoImpl implements AdministratorTypeDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private AdministratorTypeMapper administratorTypeMapper;

    //查询全部
    public List<AdministratorType> getAllAdministratorTypes() throws Exception {
        return administratorTypeMapper.selectByExample(null);
    }

    //按主键查询
    @Override
    public AdministratorType getAdministratorTypeByAno(String ano) throws Exception {
        return administratorTypeMapper.selectByPrimaryKey(ano);
    }

    //分页查询
    @Override
    public List<AdministratorType> getAdministratorTypeByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) administratorTypeMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return administratorTypeMapper.getAdministratorTypeByPage(map);
    }

    //删除
    @Override
    public Integer deleteAdministratorTypeByAno(String ano) {
        return administratorTypeMapper.deleteByPrimaryKey(ano);
    }

    //修改
    @Override
    public Integer updateAdministratorTypeByAno(AdministratorType administratorType) {
        return administratorTypeMapper.updateByPrimaryKey(administratorType);
    }


    //增加
    @Override
    public Integer addAdministratorType(AdministratorType administratorType) {
        administratorType.setAno(initId());
        return administratorTypeMapper.insertSelective(administratorType);
    }

    public String initId() {
        String maxAno = administratorTypeMapper.getMaxAno();
        maxAno = Optional.ofNullable(maxAno).orElse("0000");
        return GenNumTools.initId("ad", 4, maxAno);
    }

}